import { CCUtils } from "../Framework/scripts/CCUtils";
import { Toolkit } from "../Framework/scripts/Toolkit";
import Watcher from "../Framework/scripts/Watcher";
import CookingTiledom from "./CookingTiledom";
import Item from "./Item";
import ItemPanel from "./ItemPanel";
const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemFactory extends cc.Component {

    /** 6x5 */
    onLoad() {
        Watcher.on(CookingTiledom.EVENT_CREATE_ITEM, this, this.setDesktop);
        Watcher.on(ItemPanel.EVENT_FLY_ITEM, this, this.removeItem);
    }

    /** 设置桌面 */
    setDesktop(config, itemprefab: cc.Prefab) {
        //初始化关卡道具
        this.initItems(config);
        //随机生成测试
        this.randomSpawn(itemprefab);
    }

    items = [];
    isFirst = false;
    initItems( config ){
        this.items = [];
        let weight = config.weight;
        let sum = config.sum;
        let items = config.item_list;
        this.isFirst = config.first;

        for(let id in items ){
            let num = Math.ceil(items[id] * sum / weight);
            while( num % 3 != 0 ) ++num;
            let gid = parseInt(id);
            for(let i = 0;i<num;i++)
                this.items.push( gid );
        }

        let len = this.items.length - 36;
        for( let i = 0;i<len;i++ ) this.items.pop();
        Toolkit.logTrace(`初始化配置成功 道具总数: ${this.items.length}`);
    }


    //获取道具
    getItem(){
        let randomIndex = Toolkit.RandomInt(this.items.length);
        let id = this.items.splice(randomIndex,1)[0];
        return id;
    }

    unit_w = 0;
    unit_h = 0;
    //随机生成
    randomSpawn(itemprefab) {

        let _w = this.node.width / 6;
        let _h = this.node.height / 5;
        this.unit_w = _w;
        this.unit_h = _h;
        let _top = (this.node.height - _h) * 0.5;
        let _left = (-this.node.width + _w) * 0.5;

        //外边框向中心生成
        for (let i = 0, _i = 5; i < _i; i++, _i--) {
            for (let j = 0, _j = 4; j < _j; j++, _j--) {
                //左上
                let inst = cc.instantiate(itemprefab);
                this.addItem(_left, _top, _w, _h, i, j, inst);
                //右下
                inst = cc.instantiate(itemprefab);
                this.addItem(_left, _top, _w, _h, _i, _j, inst);
                //左下
                inst = cc.instantiate(itemprefab);
                this.addItem(_left, _top, _w, _h, i, _j, inst);
                //右下
                inst = cc.instantiate(itemprefab);
                this.addItem(_left, _top, _w, _h, _i, j, inst);
            }
        }
        //中间横排
        for (let i = 0; i < 6; i++) {
            let inst = cc.instantiate(itemprefab);
            this.addItem(_left, _top, _w, _h, i, 2, inst);
        }

        //第二层
        _top = this.itemMap[0][1][1].node.y - _h * 0.5;
        _left = this.itemMap[0][1][1].node.x + _w * 0.5;
        for (let i = 0; i < 3; i++) {
            for (let j = 0; j < 2; j++) {
                let inst = cc.instantiate(itemprefab);
                this.addItem(_left, _top, _w, _h, i, j, inst, 1);
                inst.zIndex = 2;
            }
        }
        this.refreshState();

        Toolkit.logTrace(`Item创建完成: ${this.node.childrenCount}`);
    }

    refreshLock = false;
    refreshState() {
        if (this.refreshLock) return;
        this.refreshLock = true;

        const items1 = this.itemMap[0];
        const items2 = this.itemMap[1];
        const bound_r = (this.unit_h + this.unit_w) * 0.5;

        //底层
        for (let lines1 of items1) {
            for (let item1 of lines1) {
                item1.interaction = true;
            }
        }

        //上层
        for (let lines of items2) {
            for (let item of lines) {

                //底层
                for (let lines1 of items1) {
                    for (let item1 of lines1) {

                        if (CCUtils.distance(item1.node, item.node) <= bound_r) {
                            item1.interaction = false;
                            this.refreshLock = false;
                        }
                    }
                }

            }
        }
    }

    removeItem(item: Item) {
        this.itemMap[item.layer][item.coord.y].splice(
            this.itemMap[item.layer][item.coord.y].indexOf(item), 1
        );

        this.refreshState();
    }

    itemMap: Array<Array<Array<Item>>> = [];
    addItem(_left, _top, _w, _h, i, j, inst, layer = 0) {

        this.node.addChild(inst);
        inst.x = _left + i * _w;
        inst.y = _top - j * _h;

        let s: Item = inst.getComponent(Item);
        s.setIcon(this.getItem());

        s.layer = layer;
        s.coord = { x: i, y: j };
        if (!this.itemMap[layer]) {
            this.itemMap[layer] = [];
        }
        if (!this.itemMap[layer][j]) {
            this.itemMap[layer][j] = [];
        }
        this.itemMap[layer][j][i] = s;
    }

}
